package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmb.ebank.pay.CmbB2bPayResult;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
/** 
 * @ClassName: CmbB2bServerRes 
 * @Description: 招商银行b2b 处理类
 * @author w_jiqing 
 * @date 2016年8月29日 下午4:41:20  
 */ 
@Controller
@Scope("prototype")
public class CmbB2bServerRes extends BankResBase{
    private static Log logger = LogFactory.getLog(CmbB2bServerRes.class);
    
    @RequestMapping(value= "/cmbB2bServerRes")
    protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response){
        logger.error("--------------- cmbB2bServerRes bankback ---------------");
        return super.execute(request, response);
        
    }

    @Override
    protected BaseMessage buildMessage(HttpServletRequest request) {
        String content =request.getParameter("content");        
        String plain =content.substring(content.indexOf("<NCB2BFIN>"), content.indexOf("</NCB2BFIN>")+11);
        XStream xstream = new XStream(new DomDriver("UTF-8"));
        xstream.processAnnotations(CmbB2bPayResult.class);
        CmbB2bPayResult messageB2b =(CmbB2bPayResult) xstream.fromXML(plain);
        messageB2b.setPlainMessage(content);
        messageB2b.setSignMessage(request.getParameter("signature"));
        messageB2b.setAmount(new BigDecimal(messageB2b.getEndamt()).divide(new BigDecimal(100)));
        messageB2b.setBankOrderId(messageB2b.getReford());
        messageB2b.setBank(BankConstants.BANK_LOGO_CMB);
        messageB2b.setBankBusProd(BisBankBusinessProd._01);                
        return messageB2b;
    }

    @Override
    protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
        OrderInfo orderInfo =new OrderInfo();
        CmbB2bPayResult message =(CmbB2bPayResult) result.getModel("res");
        if(message != null){
            orderInfo.setOrderId(message.getOrderId());
            orderInfo.setAmt(message.getAmount().toString());
            orderInfo.setNotifyNum("1");
            orderInfo.setPayStatus("");
            orderInfo.setRetCode("00");
            orderInfo.setRetMsg("");
            orderInfo.setNotify(true);
        }
        orderInfo.setBankName("cmb");
        return orderInfo;
    }

    @Override
    protected boolean checkParameters(HttpServletRequest request) {
        if(StringUtils.isEmpty("content")||StringUtils.isEmpty("signature")){
            return false;
        }
        return true;
    }

}
